package juanca.mole;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import ClasesExtras.handlerAlertMessage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RadioButton;

public class ChequeInspecMp extends Activity {

	private boolean estaLimpia;
	private boolean tienePlaga;
	private boolean libreParticulas;
	private boolean averiada;
	private boolean contamina;
	private boolean color;
	private boolean olor;
	private boolean sabor;
	private boolean textura;
	private boolean valvula;
	private boolean sello;
	private String obsEstaLimpia;
	private String obsTienePlaga;
	private String obsLibreParticulas;
	private String obsAveriada;
	private String obsContamina;
	private String obsColor;
	private String obsOlor;
	private String obsSabor;
	private String obsTextura;
	private String obsValvula;
	private String obsSello;
	private String medPh;
	private String consistencia;
	private String acidez;
	private String brix;
	private String idChequeoInspeccion;

	private handlerAlertMessage manejadorMensajes;
	private Activity activity;
	private ChequeInspecMp self = this;
	public static final String STRING_EMPTY = "";
	private static final String URL_REGISTROS_CHEQUEO_INSPECCION = "http://10.1.1.174:3000/chequeos_inspeccion.json";

	private EditText etObsPre1;
	private EditText etObsPre2;
	private EditText etObsPre3;
	private EditText etObsPre4;
	private EditText etObsPre5;
	private EditText etObsPre6;
	private EditText etObsPre7;
	private EditText etObsPre8;
	private EditText etObsPre9;
	private EditText etObsPre10;
	private EditText etObsPre11;
	private EditText etMedPh;
	private EditText etConsistenciaPre;
	private EditText etAcidezPre;
	private EditText etBrixPre;

	/**
	 * @return the idChequeoInspeccion
	 */
	public String getIdChequeoInspeccion() {
		return idChequeoInspeccion;
	}

	/**
	 * @param idChequeoInspeccion
	 *            the idChequeoInspeccion to set
	 */
	public void setIdChequeoInspeccion(String idChequeoInspeccion) {
		this.idChequeoInspeccion = idChequeoInspeccion;
	}

	/**
	 * @return the brix
	 */
	public String getBrix() {
		return brix;
	}

	/**
	 * @param brix
	 *            the brix to set
	 */
	public void setBrix(String brix) {
		this.brix = brix;
	}

	/**
	 * @return the acidez
	 */
	public String getAcidez() {
		return acidez;
	}

	/**
	 * @param acidez
	 *            the acidez to set
	 */
	public void setAcidez(String acidez) {
		this.acidez = acidez;
	}

	/**
	 * @return the consistencia
	 */
	public String getConsistencia() {
		return consistencia;
	}

	/**
	 * @param consistencia
	 *            the consistencia to set
	 */
	public void setConsistencia(String consistencia) {
		this.consistencia = consistencia;
	}

	/**
	 * @return the medPh
	 */
	public String getMedPh() {
		return medPh;
	}

	/**
	 * @param medPh
	 *            the medPh to set
	 */
	public void setMedPh(String medPh) {
		this.medPh = medPh;
	}

	/**
	 * @return the obsSello
	 */
	public String getObsSello() {
		return obsSello;
	}

	/**
	 * @param obsSello
	 *            the obsSello to set
	 */
	public void setObsSello(String obsSello) {
		this.obsSello = obsSello;
	}

	/**
	 * @return the obsValvula
	 */
	public String getObsValvula() {
		return obsValvula;
	}

	/**
	 * @param obsValvula
	 *            the obsValvula to set
	 */
	public void setObsValvula(String obsValvula) {
		this.obsValvula = obsValvula;
	}

	/**
	 * @return the obsTextura
	 */
	public String getObsTextura() {
		return obsTextura;
	}

	/**
	 * @param obsTextura
	 *            the obsTextura to set
	 */
	public void setObsTextura(String obsTextura) {
		this.obsTextura = obsTextura;
	}

	/**
	 * @return the obsSabor
	 */
	public String getObsSabor() {
		return obsSabor;
	}

	/**
	 * @param obsSabor
	 *            the obsSabor to set
	 */
	public void setObsSabor(String obsSabor) {
		this.obsSabor = obsSabor;
	}

	/**
	 * @return the obsOlor
	 */
	public String getObsOlor() {
		return obsOlor;
	}

	/**
	 * @param obsOlor
	 *            the obsOlor to set
	 */
	public void setObsOlor(String obsOlor) {
		this.obsOlor = obsOlor;
	}

	/**
	 * @return the obsColor
	 */
	public String getObsColor() {
		return obsColor;
	}

	/**
	 * @param obsColor
	 *            the obsColor to set
	 */
	public void setObsColor(String obsColor) {
		this.obsColor = obsColor;
	}

	/**
	 * @return the obsContamina
	 */
	public String getObsContamina() {
		return obsContamina;
	}

	/**
	 * @param obsContamina
	 *            the obsContamina to set
	 */
	public void setObsContamina(String obsContamina) {
		this.obsContamina = obsContamina;
	}

	/**
	 * @return the obsAveriada
	 */
	public String getObsAveriada() {
		return obsAveriada;
	}

	/**
	 * @param obsAveriada
	 *            the obsAveriada to set
	 */
	public void setObsAveriada(String obsAveriada) {
		this.obsAveriada = obsAveriada;
	}

	/**
	 * @return the obsLibreParticulas
	 */
	public String getObsLibreParticulas() {
		return obsLibreParticulas;
	}

	/**
	 * @param obsLibreParticulas
	 *            the obsLibreParticulas to set
	 */
	public void setObsLibreParticulas(String obsLibreParticulas) {
		this.obsLibreParticulas = obsLibreParticulas;
	}

	/**
	 * @return the obsTienePlaga
	 */
	public String getObsTienePlaga() {
		return obsTienePlaga;
	}

	/**
	 * @param obsTienePlaga
	 *            the obsTienePlaga to set
	 */
	public void setObsTienePlaga(String obsTienePlaga) {
		this.obsTienePlaga = obsTienePlaga;
	}

	/**
	 * @return the obsEstaLimpia
	 */
	public String getObsEstaLimpia() {
		return obsEstaLimpia;
	}

	/**
	 * @param obsEstaLimpia
	 *            the obsEstaLimpia to set
	 */
	public void setObsEstaLimpia(String obsEstaLimpia) {
		this.obsEstaLimpia = obsEstaLimpia;
	}

	/**
	 * @return the sello
	 */
	public boolean isSello() {
		return sello;
	}

	/**
	 * @param sello
	 *            the sello to set
	 */
	public void setSello(boolean sello) {
		this.sello = sello;
	}

	/**
	 * @return the valvula
	 */
	public boolean isValvula() {
		return valvula;
	}

	/**
	 * @param valvula
	 *            the valvula to set
	 */
	public void setValvula(boolean valvula) {
		this.valvula = valvula;
	}

	/**
	 * @return the textura
	 */
	public boolean isTextura() {
		return textura;
	}

	/**
	 * @param textura
	 *            the textura to set
	 */
	public void setTextura(boolean textura) {
		this.textura = textura;
	}

	/**
	 * @return the sabor
	 */
	public boolean isSabor() {
		return sabor;
	}

	/**
	 * @param sabor
	 *            the sabor to set
	 */
	public void setSabor(boolean sabor) {
		this.sabor = sabor;
	}

	/**
	 * @return the olor
	 */
	public boolean isOlor() {
		return olor;
	}

	/**
	 * @param olor
	 *            the olor to set
	 */
	public void setOlor(boolean olor) {
		this.olor = olor;
	}

	/**
	 * @return the color
	 */
	public boolean isColor() {
		return color;
	}

	/**
	 * @param color
	 *            the color to set
	 */
	public void setColor(boolean color) {
		this.color = color;
	}

	/**
	 * @return the contamina
	 */
	public boolean isContamina() {
		return contamina;
	}

	/**
	 * @param contamina
	 *            the contamina to set
	 */
	public void setContamina(boolean contamina) {
		this.contamina = contamina;
	}

	/**
	 * @return the averiada
	 */
	public boolean isAveriada() {
		return averiada;
	}

	/**
	 * @param averiada
	 *            the averiada to set
	 */
	public void setAveriada(boolean averiada) {
		this.averiada = averiada;
	}

	/**
	 * @return the libreParticulas
	 */
	public boolean isLibreParticulas() {
		return libreParticulas;
	}

	/**
	 * @param libreParticulas
	 *            the libreParticulas to set
	 */
	public void setLibreParticulas(boolean libreParticulas) {
		this.libreParticulas = libreParticulas;
	}

	/**
	 * @return the tienePlaga
	 */
	public boolean isTienePlaga() {
		return tienePlaga;
	}

	/**
	 * @param tienePlaga
	 *            the tienePlaga to set
	 */
	public void setTienePlaga(boolean tienePlaga) {
		this.tienePlaga = tienePlaga;
	}

	/**
	 * @return the estaLimpia
	 */
	public boolean isEstaLimpia() {
		return estaLimpia;
	}

	/**
	 * @param estaLimpia
	 *            the estaLimpia to set
	 */
	public void setEstaLimpia(boolean estaLimpia) {
		this.estaLimpia = estaLimpia;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_cheque_inspec_mp);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		etObsPre1 = (EditText) findViewById(R.id.etObsPre1);
		etObsPre2 = (EditText) findViewById(R.id.etObsPre2);
		etObsPre3 = (EditText) findViewById(R.id.etObsPre3);
		etObsPre4 = (EditText) findViewById(R.id.etObsPre4);
		etObsPre5 = (EditText) findViewById(R.id.etObsPre5);
		etObsPre6 = (EditText) findViewById(R.id.etObsPre6);
		etObsPre7 = (EditText) findViewById(R.id.etObsPre7);
		etObsPre8 = (EditText) findViewById(R.id.etObsPre8);
		etObsPre9 = (EditText) findViewById(R.id.etObsPre9);
		etObsPre10 = (EditText) findViewById(R.id.etObsPre10);
		etObsPre11 = (EditText) findViewById(R.id.etObsPre11);
		etMedPh = (EditText) findViewById(R.id.etMedPh);
		etConsistenciaPre = (EditText) findViewById(R.id.etConsistenciaPre);
		etAcidezPre = (EditText) findViewById(R.id.etAcidezPre);
		etBrixPre = (EditText) findViewById(R.id.etBrixPre);

		manejadorMensajes = new handlerAlertMessage();
		activity = this;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.layout_cheque_inspec_mp, menu);
		return true;
	}

	// metodo de radiobutton pre1
	public void ifResPre1(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre1:
			if (checked) {

				this.setEstaLimpia(checked);

			}
			break;
		case R.id.rbNoResPre1:
			if (checked) {
				this.setEstaLimpia(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre2
	public void ifResPre2(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre2:
			if (checked) {

				this.setTienePlaga(checked);

			}
			break;
		case R.id.rbNoResPre2:
			if (checked) {
				this.setTienePlaga(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre3
	public void ifResPre3(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre3:
			if (checked) {

				this.setLibreParticulas(checked);

			}
			break;
		case R.id.rbNoResPre3:
			if (checked) {
				this.setLibreParticulas(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre4
	public void ifResPre4(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre4:
			if (checked) {

				this.setAveriada(checked);

			}
			break;
		case R.id.rbNoResPre4:
			if (checked) {
				this.setAveriada(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre5
	public void ifResPre5(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre5:
			if (checked) {

				this.setContamina(checked);

			}
			break;
		case R.id.rbNoResPre5:
			if (checked) {
				this.setContamina(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre6
	public void ifResPre6(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre6:
			if (checked) {

				this.setColor(checked);
			}
			break;
		case R.id.rbNoResPre6:
			if (checked) {
				this.setColor(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre7
	public void ifResPre7(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre7:
			if (checked) {

				this.setOlor(checked);

			}
			break;
		case R.id.rbNoResPre7:
			if (checked) {
				this.setOlor(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre8
	public void ifResPre8(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre8:
			if (checked) {

				this.setSabor(checked);

			}
			break;
		case R.id.rbNoResPre8:
			if (checked) {
				this.setSabor(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre9
	public void ifResPre9(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre9:
			if (checked) {

				this.setAveriada(checked);

			}
			break;
		case R.id.rbNoResPre9:
			if (checked) {
				this.setTextura(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre10
	public void ifResPre10(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre10:
			if (checked) {

				this.setValvula(checked);

			}
			break;
		case R.id.rbNoResPre10:
			if (checked) {
				this.setValvula(!checked);

			}
			break;

		}
	}

	// metodo de radiobutton pre11
	public void ifResPre11(View v) {
		// Is the button now checked?
		boolean checked = ((RadioButton) v).isChecked();

		// Check which radio button was clicked

		switch (v.getId()) {
		case R.id.rbSiResPre11:
			if (checked) {

				this.setSello(checked);

			}
			break;
		case R.id.rbNoResPre11:
			if (checked) {
				this.setSello(!checked);

			}
			break;

		}
	}

	// metodo que lleva a la vista de inspeccion MP
	public void goInspec(View view) {
		this.validar();

	}

	// hace validacion
	public void validar() {

		if (!etObsPre1.getText().toString().equals(STRING_EMPTY))
			this.setObsEstaLimpia(etObsPre1.getText().toString());

		if (!etObsPre2.getText().toString().equals(STRING_EMPTY))
			this.setObsTienePlaga(etObsPre2.getText().toString());

		if (!etObsPre3.getText().toString().equals(STRING_EMPTY))
			this.setObsLibreParticulas(etObsPre3.getText().toString());

		if (!etObsPre4.getText().toString().equals(STRING_EMPTY))
			this.setObsAveriada(etObsPre4.getText().toString());

		if (!etObsPre5.getText().toString().equals(STRING_EMPTY))
			this.setObsContamina(etObsPre5.getText().toString());

		if (!etObsPre6.getText().toString().equals(STRING_EMPTY))
			this.setObsColor(etObsPre6.getText().toString());

		if (!etObsPre7.getText().toString().equals(STRING_EMPTY))
			this.setObsOlor(etObsPre7.getText().toString());

		if (!etObsPre8.getText().toString().equals(STRING_EMPTY))
			this.setObsSabor(etObsPre8.getText().toString());

		if (!etObsPre9.getText().toString().equals(STRING_EMPTY))
			this.setObsTextura(etObsPre9.getText().toString());

		if (!etObsPre10.getText().toString().equals(STRING_EMPTY))
			this.setObsValvula(etObsPre10.getText().toString());

		if (!etObsPre11.getText().toString().equals(STRING_EMPTY))
			this.setObsSello(etObsPre11.getText().toString());

		if (!etMedPh.getText().toString().equals(STRING_EMPTY))
			this.setMedPh(etMedPh.getText().toString());

		if (!etConsistenciaPre.getText().toString().equals(STRING_EMPTY))
			this.setConsistencia(etConsistenciaPre.getText().toString());

		if (!etAcidezPre.getText().toString().equals(STRING_EMPTY))
			this.setAcidez(etAcidezPre.getText().toString());

		if (!etBrixPre.getText().toString().equals(STRING_EMPTY))
			this.setBrix(etBrixPre.getText().toString());

		new Thread(new Runnable() {
			@Override
			public void run() {
				self.envioBaseDatos();
				Log.e("ServicioRest", "entra envio basedatos");

			}
		}).start();

	}

	/* metodo que envia los datos al servidor */
	public void envioBaseDatos() {

		HttpClient httpClient = new DefaultHttpClient();
		// URL del servidor para guardar los datos de lavadoMaquina
		HttpPost post = new HttpPost(URL_REGISTROS_CHEQUEO_INSPECCION);

		try {
			// Construimos el objeto cliente en formato JSON
			JSONObject dato = new JSONObject();

			dato.put("chequeo_limpieza", this.isEstaLimpia());
			dato.put("chequeo_plagas", this.isTienePlaga());
			dato.put("chequeo_paticulas_ajenas", this.isLibreParticulas());
			dato.put("chequeo_averiada", this.isAveriada());
			dato.put("chequeo_contamina", this.isContamina());
			dato.put("chequeo_color", this.isColor());
			dato.put("chequeo_olor", this.isOlor());
			dato.put("chequeo_sabor", this.isSabor());
			dato.put("chequeo_textura", this.isTextura());
			dato.put("chequeo_valvula", this.isValvula());
			dato.put("chequeo_sello", this.isTextura());
			dato.put("chequeo_medicion_ph", this.getMedPh());
			dato.put("chequeo_brix", this.getBrix());
			dato.put("chequeo_consistencia", this.getConsistencia());
			dato.put("chequeo_acidez", this.getAcidez());

			// obs
			dato.put("obs_chequeo_limpieza", this.getObsEstaLimpia());
			dato.put("obs_chequeo_plagas", this.getObsTienePlaga());
			dato.put("obs_chequeo_paticulas_ajenas",
					this.getObsLibreParticulas());
			dato.put("obs_chequeo_averiada", this.getObsAveriada());
			dato.put("obs_chequeo_contamina", this.getObsContamina());
			dato.put("obs_chequeo_color", this.getObsColor());
			dato.put("obs_chequeo_olor", this.getObsOlor());
			dato.put("obs_chequeo_sabor", this.getObsSabor());
			dato.put("obs_chequeo_textura", this.getObsTextura());
			dato.put("obs_chequeo_valvula", this.getObsValvula());
			dato.put("obs_chequeo_sello", this.getObsSello());

			StringEntity entity = new StringEntity(dato.toString());
			post.setHeader("Content-Type", "application/json; chartset=UTF-8");
			post.setHeader("Accept", "application/json");
			post.setEntity(entity);
			HttpResponse resp = httpClient.execute(post);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					resp.getEntity().getContent(), "UTF-8"));
			String json = reader.readLine();
			JSONTokener tokener = new JSONTokener(json);
			JSONArray finalResult = new JSONArray(tokener);

			Log.e("ServicioRest", finalResult.toString());

			if (finalResult.getJSONObject(0).getString("estado").equals("true")) {
				String idCehqueoInspec = finalResult.getJSONObject(0)
						.getString("id");
				this.setIdChequeoInspeccion(idCehqueoInspec);

				runOnUiThread(new Runnable() {
					public void run() {
						Intent intent = new Intent();
						intent.setClass(activity, InspaccionMp.class);
						intent.putExtra("IdChequeoInspeccion",
								self.getIdChequeoInspeccion());
						startActivity(intent);

					}
				});

			} else {
				runOnUiThread(new Runnable() {
					public void run() {

						manejadorMensajes.alertMessageOneButton(activity,
								R.string.tituloError,
								R.string.msjErServerRetornaRespt,
								R.string.btnAceptar);
					}
				});

			}

		} catch (Exception e) {

			runOnUiThread(new Runnable() {
				public void run() {

					AlertDialog.Builder mensajeEmergente = new AlertDialog.Builder(
							activity);
					mensajeEmergente.setTitle(R.string.tituloError);
					mensajeEmergente
							.setMessage(R.string.msjERServerNoRetornaTrue);
					mensajeEmergente.setNeutralButton(R.string.btnAceptar,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {

								}
							});

					mensajeEmergente.create();
					mensajeEmergente.show();
					Log.e("ServicioRest",
							"el servidor no retorna true y esta en el mensaje en pantalla");
				}
			});
		}

	}
}
